define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'jstree'], function ($, undefined, Backend, Table, Form, undefined) {
    //读取选中的条目
    $.jstree.core.prototype.get_all_checked = function (full) {
        var obj = this.get_selected(), i, j;
        for (i = 0, j = obj.length; i < j; i++) {
            obj = obj.concat(this.get_node(obj[i]).parents);
        }
        obj = $.grep(obj, function (v, i, a) {
            return v != '#';
        });
        obj = obj.filter(function (itm, i, a) {
            return i == a.indexOf(itm);
        });
        return full ? $.map(obj, $.proxy(function (i) {
            return this.get_node(i);
        }, this)) : obj;
    };


    $(function () {
        if ($('input[name=role_key]').length > 0) {
            $.post('/sys/role/tree', {'role_key': role.role_key}, function (ret) {
                console.log(ret);
                $("#treeview").jstree("destroy");

                var ndata = [];
                var data = ret.data;
                var nresourceIdList = [];
                var resourceIdList = resourceIds.split(",");
                for (var resourceIdListKey of resourceIdList) {
                    nresourceIdList.push(parseInt(resourceIdListKey));
                }
                console.log(nresourceIdList);
                $.each(data, function (index, item, arr) {
                    var itemId = item.id;
                    item.type = 'file';
                    item.state = {};
                    item.icon="fa fa-folder-open"
                    if (nresourceIdList.indexOf(itemId) > -1) {
                        item.state = {
                            selected: true,
                            opened: true

                        };
                    } else {
                        item.state={
                            selected: false,
                            opened: true
                        }
                    }
                    ndata.push(item);
                });
                console.log(ndata);
                Controller.api.rendertree(ndata);


            }, 'json');
        }

    })
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: '/sys/role/data',
                    add_url: '/sys/role/add',
                    edit_url: '/sys/role/edit',
                    del_url: '/sys/role/del',
                    multi_url: 'data/multi.json',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                escape: false,
                columns: [
                    [
                        {field: 'state', checkbox: true, },
                        {field: 'id', title: 'ID'},
                        {field: 'role_key', title: __('角色键')},
                        {field: 'role_desc', title: __('描述'), align: 'left'},
                        {field: 'operate', title: __('Operate'), events: Table.api.events.operate, formatter: function (value, row, index) {
                                return Table.api.formatter.operate.call(this, value, row, index, table);
                            }}
                    ]
                ],
                pagination: false,
                search: false,
                commonSearch: false,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);//当内容渲染完成后

        },
        add: function () {
            Form.api.bindevent($("form[role=form]"));
            Controller.api.bindevent();
        },
        edit: function () {
            Form.api.bindevent($("form[role=form]"));
            Controller.api.bindevent();
        },
        api: {
            refreshrules: function () {
                if ($("#treeview").size() > 0) {
                    var r = $("#treeview").jstree("get_all_checked");
                    $("input[name=resources]").val(r.join(','));
                }
                return true;
            },
            bindevent: function () {
                Form.api.custom.refreshrules = Controller.api.refreshrules;
                //全选和展开
                $(document).on("click", "#checkall", function () {
                    $("#treeview").jstree($(this).prop("checked") ? "check_all" : "uncheck_all");
                });
                $(document).on("click", "#expandall", function () {
                    $("#treeview").jstree($(this).prop("checked") ? "open_all" : "close_all");
                });
            },
            rendertree: function (content) {
                $("#treeview")
                        .on('redraw.jstree', function (e) {
                            $(".layer-footer").attr("domrefresh", Math.random());
                        })
                        .jstree({
                            "themes": {"stripes": true},
                            "core":{
                                "themes" : { "dots": true,"icons":true ,"stripes":false},
                            },
                            "checkbox": {
                                "keep_selected_style": true,
                                'three_state' : false,
                            },
                            "types": {
                                "root": {
                                    "icon": "fa fa-folder-open",
                                },
                                "menu": {
                                    "icon": "fa fa-folder-open",
                                },
                                "file": {
                                    "icon": "fa fa-diamond",
                                }
                            },
                            "plugins": ["checkbox", "types"],
                            "core": {
                                'check_callback': false,
                                "data": content
                            }
                        });
            }
        }
    };
    return Controller;
});